using System.Diagnostics;
using System.ServiceModel;

using Microsoft.Services.TfsAlert.AppCode;
using Microsoft.Services.TfsAlert.AppCode.NotificationEvents;
using System;
using System.Text;

namespace Microsoft.Services.TfsAlert.AppCode
{
    [ServiceBehavior(
        InstanceContextMode = InstanceContextMode.Single,
        IncludeExceptionDetailInFaults = true,
        ValidateMustUnderstand = false )]
    public class NotificationReciever : INotificationReciever
    {
        public static event MessageAlertDelegate MessageAlert;

        public void Notify( string eventXml, string tfsIdentityXml, SubscriptionInfo SubscriptionInfo )
        {
            try
            {
                NotificationEventFactory notificationEventFactory = new NotificationEventFactory();
                INotificationEvent notficationEvent = notificationEventFactory.CreateEvent( eventXml );

                NotificationReciever.MessageAlert( this, new MessageAlertEventArgs( notficationEvent ) );

                if ( Instrumentation.GeneralTraceSwitch.TraceInfo )
                {
                    Trace.TraceInformation( eventXml );
                }
            }
            catch ( Exception ex )
            {
                StringBuilder errorMessage = new StringBuilder();
                errorMessage.AppendFormat( "TfsAlert Errored in Notify: \n{0}", ex.Message );
                errorMessage.AppendFormat( "Event XML: \n{0}", eventXml );

                Trace.TraceInformation( ex.Message );
            }
        }
    }
}